package com.justcan.demo.cartoon.utils.anim.touch;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.justcan.demo.cartoon.utils.anim.AnimObjectGroup;
import com.justcan.demo.cartoon.utils.anim.AnimView;


public class TouchAnimView extends AnimView implements IAnimTouchable {

	/**
	 * 分发成功的AnimObjectGroup
	 */
	private TouchAnimObjectGroup mTargetAnimObjectGroup = null;

	public TouchAnimView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public TouchAnimView(Context context) {
		super(context);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return onTouch(event);
	}

	@Override
	public boolean onTouch(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN || mTargetAnimObjectGroup == null) {
			for (int i = 0; i < mAnimObjectGroups.size(); i++) {
				AnimObjectGroup object = mAnimObjectGroups.get(i);
				if (object instanceof TouchAnimObjectGroup) {
					if (((TouchAnimObjectGroup) object).onTouch(event)) {
						// 接受事件
						mTargetAnimObjectGroup = (TouchAnimObjectGroup) object;
						return true;
					}
				}
			}
		} else if (mTargetAnimObjectGroup != null) {
			return mTargetAnimObjectGroup.onTouch(event);
		}
		return false;
	}
}
